<template>
  <view class="container">
    <!-- 订单状态筛选 -->
    <view class="filter-bar">
      <view 
        class="filter-item" 
        :class="{ 'active': currentStatus === '' }"
        @click="changeStatus('')"
      >
        <text>全部</text>
      </view>
      <view 
        class="filter-item" 
        :class="{ 'active': currentStatus === 0 }"
        @click="changeStatus(0)"
      >
        <text>待支付</text>
      </view>
      <view 
        class="filter-item" 
        :class="{ 'active': currentStatus === 1 }"
        @click="changeStatus(1)"
      >
        <text>已支付</text>
      </view>
    </view>

    <!-- 订单列表 -->
    <view class="order-list">
      <view v-if="loading" class="loading">
        <text>加载中...</text>
      </view>
      
      <view v-else-if="orderList.length === 0" class="empty">
        <text class="empty-icon">📋</text>
        <text class="empty-text">暂无订单</text>
      </view>
      
      <view v-else class="orders">
        <view 
          class="order-item" 
          v-for="item in orderList" 
          :key="item.id"
          @click="goToDetail(item.id)"
        >
          <!-- 订单头部 -->
          <view class="order-header">
            <text class="order-no">订单号：{{ item.id }}</text>
            <text class="order-status" :class="getStatusClass(item.statue)">
              {{ getStatusText(item.statue) }}
            </text>
          </view>
          
          <!-- 商品信息 -->
          <view class="product-info">
            <text class="product-name">{{ item.productName }}</text>
            <text class="merchant-name">商家：{{ item.merchantId }}</text>
            <text class="quantity">数量：{{ item.num }}件</text>
          </view>
          
          <!-- 价格信息 -->
          <view class="price-info">
            <view class="price-item">
              <text class="price-label">积分支付：</text>
              <text class="price-value">{{ formatPrice(item.pointAmount) }}</text>
            </view>
            <view class="price-item">
              <text class="price-label">支付宝支付：</text>
              <text class="price-value">{{ formatPrice(item.payAmount) }}</text>
            </view>
            <view class="price-item total">
              <text class="price-label">订单总额：</text>
              <text class="price-value">{{ formatPrice(item.amount) }}</text>
            </view>
          </view>
          
          <!-- 时间信息 -->
          <view class="time-info">
            <text class="create-time">{{ formatTime(item.createTime) }}</text>
          </view>
          
          <!-- 操作按钮 -->
          <view class="order-actions">
            <button 
              v-if="item.statue === 0" 
              class="pay-btn"
              @click.stop="handlePay(item)"
            >
              去支付
            </button>
            <button 
              class="detail-btn"
              @click.stop="goToDetail(item.id)"
            >
              查看详情
            </button>
          </view>
        </view>
      </view>
      
      <!-- 加载更多 -->
      <view v-if="hasMore && !loading" class="load-more">
        <button class="load-more-btn" @click="loadMore">加载更多</button>
      </view>
    </view>
  </view>
</template>

<script>
import { getMyOrders } from '@/api/order.js'

export default {
  data() {
    return {
      orderList: [], // 订单列表
      currentStatus: '', // 当前筛选状态
      loading: false, // 加载状态
      hasMore: true, // 是否还有更多
      pageIndex: 1, // 当前页码
      pageSize: 10, // 每页条数
      total: 0 // 总数量
    }
  },
  onLoad() {
    this.loadOrderList()
  },
  onShow() {
    // 每次显示页面时刷新订单列表
    this.refresh()
  },
  onPullDownRefresh() {
    // 下拉刷新
    this.refresh()
  },
  onReachBottom() {
    // 上拉加载更多
    if (this.hasMore && !this.loading) {
      this.loadMore()
    }
  },
  methods: {
    // 加载订单列表
    async loadOrderList(isRefresh = false) {
      if (this.loading) return
      
      this.loading = true
      
      try {
        const params = {
          pageIndex: isRefresh ? 1 : this.pageIndex,
          pageSize: this.pageSize
        }
        
        // 如果有状态筛选，添加到参数中
        if (this.currentStatus !== '') {
          params.statue = this.currentStatus
        }
        
        const res = await getMyOrders(params)
        
        if (isRefresh) {
          this.orderList = res.data.list || []
          this.pageIndex = 1
        } else {
          this.orderList = [...this.orderList, ...(res.data.list || [])]
        }
        
        this.total = res.data.total || 0
        this.hasMore = this.orderList.length < this.total
        
        if (!isRefresh) {
          this.pageIndex++
        }
        
      } catch (error) {
        console.error('加载订单列表失败:', error)
        uni.showToast({
          title: '加载失败',
          icon: 'none'
        })
      } finally {
        this.loading = false
        uni.stopPullDownRefresh()
      }
    },
    
    // 刷新
    refresh() {
      this.pageIndex = 1
      this.hasMore = true
      this.loadOrderList(true)
    },
    
    // 加载更多
    loadMore() {
      this.loadOrderList()
    },
    
    // 切换状态筛选
    changeStatus(status) {
      this.currentStatus = status
      this.refresh()
    },
    
    // 跳转订单详情
    goToDetail(orderId) {
      uni.navigateTo({
        url: `/pages/order/detail?id=${orderId}`
      })
    },
    
    // 去支付
    handlePay(order) {
      uni.navigateTo({
        url: `/pages/order/pay?orderId=${order.id}&amount=${order.amount}&pointAmount=${order.amount}`
      })
    },
    
    // 获取状态文字
    getStatusText(status) {
      const statusMap = {
        0: '待支付',
        1: '已支付'
      }
      return statusMap[status] || '未知状态'
    },
    
    // 获取状态样式类
    getStatusClass(status) {
      const classMap = {
        0: 'status-pending',
        1: 'status-paid'
      }
      return classMap[status] || ''
    },
    
    // 格式化价格
    formatPrice(price) {
      return (price / 100).toFixed(2) + '元'
    },
    
    // 格式化时间
    formatTime(time) {
      if (!time) return ''
      return time.replace(' ', '\n')
    }
  }
}
</script>

<style scoped>
.container {
  min-height: 100vh;
  background: #f5f5f5;
}

.filter-bar {
  display: flex;
  background: #fff;
  padding: 20rpx 30rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.filter-item {
  flex: 1;
  text-align: center;
  padding: 20rpx 0;
  font-size: 28rpx;
  color: #666;
  border-radius: 8rpx;
  margin: 0 10rpx;
  transition: all 0.3s;
}

.filter-item.active {
  background: #667eea;
  color: #fff;
}

.order-list {
  padding: 30rpx;
}

.loading, .empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 400rpx;
  color: #999;
}

.empty-icon {
  font-size: 80rpx;
  margin-bottom: 20rpx;
}

.empty-text {
  font-size: 28rpx;
}

.order-item {
  background: #fff;
  border-radius: 12rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.order-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
  padding-bottom: 20rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.order-no {
  font-size: 26rpx;
  color: #666;
}

.order-status {
  padding: 8rpx 16rpx;
  border-radius: 6rpx;
  font-size: 22rpx;
  font-weight: bold;
}

.status-pending {
  background: #fff7e6;
  color: #fa8c16;
}

.status-paid {
  background: #f6ffed;
  color: #52c41a;
}

.product-info {
  margin-bottom: 20rpx;
}

.product-name {
  font-size: 30rpx;
  font-weight: bold;
  color: #333;
  display: block;
  margin-bottom: 10rpx;
}

.merchant-name, .quantity {
  font-size: 24rpx;
  color: #666;
  display: block;
  margin-bottom: 5rpx;
}

.price-info {
  margin-bottom: 20rpx;
  padding: 20rpx;
  background: #f8f9fa;
  border-radius: 8rpx;
}

.price-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10rpx;
}

.price-item:last-child {
  margin-bottom: 0;
}

.price-item.total {
  border-top: 1rpx solid #e9ecef;
  padding-top: 10rpx;
  font-weight: bold;
}

.price-label {
  font-size: 26rpx;
  color: #666;
}

.price-value {
  font-size: 26rpx;
  color: #333;
}

.price-item.total .price-value {
  color: #ff6b6b;
  font-weight: bold;
}

.time-info {
  margin-bottom: 20rpx;
}

.create-time {
  font-size: 24rpx;
  color: #999;
  line-height: 1.4;
}

.order-actions {
  display: flex;
  justify-content: flex-end;
  gap: 20rpx;
}

.pay-btn {
  padding: 16rpx 32rpx;
  background: #ff6b6b;
  color: #fff;
  font-size: 24rpx;
  border-radius: 8rpx;
  border: none;
}

.pay-btn::after {
  border: none;
}

.detail-btn {
  padding: 16rpx 32rpx;
  background: #667eea;
  color: #fff;
  font-size: 24rpx;
  border-radius: 8rpx;
  border: none;
}

.detail-btn::after {
  border: none;
}

.load-more {
  text-align: center;
  padding: 30rpx 0;
}

.load-more-btn {
  padding: 20rpx 60rpx;
  background: #fff;
  color: #667eea;
  font-size: 28rpx;
  border-radius: 8rpx;
  border: 2rpx solid #667eea;
}

.load-more-btn::after {
  border: none;
}
</style>
